package com.binance.healthcenter.notification;

import com.binance.healthcenter.config.SmsNotificationConfig;
import com.binance.healthcenter.model.HealthCheckResult;
import com.binance.healthcenter.service.MessageService;
import com.google.common.collect.Maps;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Log4j2
@Service
public class SmsNotificationService {
    @Value("${com.binance.notification.sms.tplcode}")
    private String tplcode;
    @Value("${com.binance.notification.sms.defaultcellphones}")
    private String cellphones;
    @Value("${com.binance.notification.sms.userid}")
    private String userId;

    @Autowired
    private MessageService messageService;
    @Autowired
    private SmsNotificationConfig smsNotificationConfig;


    public void sendSmsForhealthCheckResultList(List<HealthCheckResult> healthCheckResultList) {
        for (HealthCheckResult healthCheckResult : healthCheckResultList) {
            try {
                log.info("SmsNotificationService.sendSmsForhealthCheckResultList serviceName={}",
                        healthCheckResult.getServiceName());
                sendSmsForhealthCheckResultListForDefinedService(healthCheckResult, healthCheckResult.getServiceName());
            } catch (Exception e) {
                log.error("SmsNotificationService.sendSmsForhealthCheckResultList Exception", e);
            }
        }
    }

    /**
     * 根据服务名字从相应的配置中获取手机号列表
     * */
    private Set<String> getCellphonesByServiceName(String serviceName) {
        List<SmsNotificationConfig.ServiceConfiguration> serviceConfigurationList = smsNotificationConfig.getServices();
        Set<String> cellphoneSet = smsNotificationConfig.getDefaultCellphoneSet();
        for (SmsNotificationConfig.ServiceConfiguration serviceConfiguration : serviceConfigurationList) {
            if (serviceConfiguration.getApplication().equalsIgnoreCase(serviceName)) {
                if (CollectionUtils.isNotEmpty(serviceConfiguration.getCellphoneSet())) {
                    cellphoneSet.addAll(serviceConfiguration.getCellphoneSet());
                }
            }
        }
        return cellphoneSet;
    }

    /**
     * 给某个特定服务发送短信
     */
    public void sendSmsForhealthCheckResultListForDefinedService(HealthCheckResult healthCheckResult,
            String serviceName) {
        Set<String> serviceCellphoneSet = getCellphonesByServiceName(serviceName);
        if (CollectionUtils.isEmpty(serviceCellphoneSet)) {
            log.info("SmsNotificationService.sendSmsForhealthCheckResultListForDefinedService cellphones empty");
            return;
        }
        for (String cellphone : serviceCellphoneSet) {
            try {
                log.info("SmsNotificationService.sendSmsForhealthCheckResultListForDefinedService cellphone={}",
                        cellphone);
                sendSms(healthCheckResult, cellphone);
            } catch (Exception e) {
                log.error("SmsNotificationService.sendSmsForhealthCheckResultListForDefinedService Exception", e);
            }
        }
    }

    /**
     * 发送多个服务，单个手机
     */
    public void sendHealthCheckResultListForSingleCellPhone(List<HealthCheckResult> healthCheckResultList,
            String cellphone) {
        for (HealthCheckResult healthCheckResult : healthCheckResultList) {
            try {
                log.info(
                        "SmsNotificationService.sendHealthCheckResultListForSingleCellPhone cellphone={},healthCheckResult={}",
                        cellphone, healthCheckResult);
                sendSms(healthCheckResult, cellphone);
            } catch (Exception e) {
                log.error("SmsNotificationService.sendHealthCheckResultListForSingleCellPhone Exception", e);
            }
        }
    }

    /**
     * 发送单个服务，单个手机
     */
    public void sendSms(HealthCheckResult healthCheckResult, String cellphone) {
        log.info("SmsNotificationService.sendSms cellphone={},healthCheckResult={}", cellphone, healthCheckResult);
        Map params = Maps.newHashMap();
        params.put("serviceName", healthCheckResult.getServiceName());
        params.put("host", healthCheckResult.getHost());
        params.put("status", healthCheckResult.getStatus());
        messageService.sendMsg(tplcode, cellphone, params, userId);
    }
}
